package com.oracle.calllog.analyze.outputformat;

import java.io.IOException;

import com.oracle.calllog.analyze.model.ContactDimBean;
import com.oracle.calllog.analyze.model.CountDuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.OutputCommitter;
import org.apache.hadoop.mapreduce.OutputFormat;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MyDBOutputFormat extends OutputFormat<ContactDimBean, CountDuration>{
    private OutputCommitter committer = null;
    @Override
    public RecordWriter<ContactDimBean, CountDuration> getRecordWriter(TaskAttemptContext context)
            throws IOException, InterruptedException {
        return new MyDBRecordWriter();
    }

    @Override
    public void checkOutputSpecs(JobContext context) throws IOException, InterruptedException {
        //输出校检(crc文件)，向数据库中写数据，无需校验即可
    }

    @Override
    public OutputCommitter getOutputCommitter(TaskAttemptContext context) throws IOException, InterruptedException {
        if(committer == null){
            String name = context.getConfiguration().get(FileOutputFormat.OUTDIR);
            Path outputPath = name == null ? null : new Path(name);
            committer = new FileOutputCommitter(outputPath, context);
        }
        return committer;
    }
}
